<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="1800"  destroy-on-close @closed="$emit('closed')" align-center >
        <el-container>
            <el-header>
                <div class="left-panel">
                    <el-button type="primary" @click="back" v-if="type">确定</el-button>
                </div>
                <div class="right-panel">
                    <div class="right-panel-search">
                        <el-input v-model="search.dye_factory" placeholder="染色厂" clearable @keyup.enter="upsearch" v-if="name == 'dye'"></el-input>
                        <el-input v-model="search.process_factory" placeholder="加工厂" clearable @keyup.enter="upsearch" v-if="name == 'process'"></el-input>
                        <el-input v-model="search.purchase_code" placeholder="订单编号" clearable @keyup.enter="upsearch"></el-input>
                        <!-- <el-input v-model="search.purchase_code" placeholder="采购编号" clearable @keyup.enter="upsearch"></el-input> -->
                        <el-select v-model="search.input_type" placeholder="订单类型" @change="upsearch" :disabled="select">
                            <el-option label="采购单" :value="1"></el-option>
                            <el-option label="染色单" :value="2"></el-option>
                            <el-option label="加工单" :value="3"></el-option>
                        </el-select>
                        <el-select v-model="search.action_type" placeholder="状态" @change="upsearch" :disabled="select">
                            <el-option label="已使用" :value="1"></el-option>
                            <el-option label="已占用" :value="2"></el-option>
                            <el-option label="已检验" :value="3"></el-option>
                            <el-option label="未检验" :value="4"></el-option>
                        </el-select>
                        <!-- <el-select v-model="search.apply_id" placeholder="供应商" filterable clearable @change="applyChange(form.apply_id)">
                            <el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
                        </el-select> -->
                        <el-date-picker v-model="search.date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
                        <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                        <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                    </div>
                </div>
            </el-header>
            <el-main class="nopadding">
                <scTable ref="table" :apiObj="apiObj" row-key="id" :params="query" height="700" border @selection-change="selectionChange" @row-dblclick="dbselectionChange" tableName="hosuerecordTable" :column="column">
                    <el-table-column type="selection" width="50" align="center"></el-table-column>
                    <template #input_type="{row}">
                        <el-tag :type="row.input_type == 1 ? 'info' : row.input_type == 2 ? 'primary' : row.input_type == 3 ? 'success' : 'warning'">{{row.input_type == 1 ? '采购单入库' : row.input_type == 2 ? '染色单入库' : row.input_type == 3 ? '加工单入库' : '其他入库'}}</el-tag>
                        <el-link :underline="false" type="primary" @click="table_show_purchase(row)">{{ row.input_type == 1 ? row.purchase?.purchase_code : row.input_type == 2 ? row.dye?.purchase_dye_code : row.process?.purchase_process_code }}</el-link>
                    </template>
                    <template #purchase_ware_code="{row}">
                        <div style="display: flex;align-items: center;">
                            <el-link :underline="false" type="primary" @click="table_show(row)">{{ row.purchase_ware_code }}</el-link><!--  @click="table_show(row)" -->
                            <el-icon class="icon" color="#1e9fff" :size="20" @click="purchase_staff(row)"><el-icon-BellFilled /></el-icon>
                        </div>
                        <div>{{row.ware_date}}({{row.ware_name}})</div>
                    </template>
                    <template #apply_name="{row}">
                        <div style="margin: 10px 0;">{{row.apply?.apply_name}}</div>
                        <div>{{row.apply?.linkman}}</div>
                        <div v-if="row.apply" v-column="'houserecord.applyprice'">单价: {{row.purchase?.price}}</div>
                    </template>
                    <template #dye_factory="{row}">
                        <div style="margin: 10px 0;">{{row.dye_factory?row.dyeFactory?.apply_name:''}}</div>
                        <div>{{row.dye_factory?row.dyeFactory?.linkman:''}}</div>
                        <div v-if="row.dye_factory" v-column="'houserecord.dyeprice'">单价: {{row.dye?.price}}</div>
                    </template>
                    <template #process_factory="{row}">
                        <div style="margin: 10px 0;">{{row.process_factory?row.processFactory?.apply_name:''}}</div>
                        <div>{{row.process_factory?row.processFactory?.linkman:''}}</div>
                        <div v-if="row.process_factory" v-column="'houserecord.processprice'">单价: {{row.process?.price}}</div>
                    </template>
                    <template #status="{row}">
                        <el-button :type="row.status == 0 ? 'warning' : row.status == 1 ? 'primary' : 'danger'" size="small" @click="purchase_check(row)" v-auth="'purchasehouse.check'">{{row.status == 0 ? '待审' : row.status == 1 ? '通过' : '退回'}}</el-button>
                    </template>
                    <template #upload="{row}">
                        <el-button type="primary" size="small" @click="purchase_upload(row)" v-if="row.status == 1" v-auth="'purchasehouse.upload'">查看</el-button>
                    </template>
                    <template #operation="scope">
                        <div class="buttonFlex">
                            <el-button size="small" @click="table_weight_note(scope.row)" v-auth="'purchasehouse.note'">设置码单</el-button>
                            <el-button size="small" @click="table_weight_note_show(scope.row)" v-auth="'purchasehouse.noteshow'">码单</el-button>
                        </div>
                        <div class="buttonFlex">
                            <el-popconfirm :title="`确定${scope.row.is_complate == 1 ? '未完' : '完成'}吗？`" @confirm="purchase_complate(scope.row)">
                                <template #reference>
                                    <el-button size="small" v-auth="'purchasehouse.complate'">{{scope.row.is_complate == 1 ? '已完成' : '未完'}}</el-button>
                                </template>
                            </el-popconfirm>
                            <el-button size="small" @click="purchase_print(scope.row)" v-auth="'purchasehouse.print'">打印</el-button>
                        </div>
                        <div class="buttonFlex">
                            <el-button type="primary" size="small" @click="table_edit(scope.row)" v-auth="'purchasehouse.edit'" v-if="scope.row.status != 1">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button type="danger" size="small" v-auth="'purchasehouse.delete'" v-if="scope.row.status != 1">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </div>
                    </template>
                    <!-- <el-table-column label="入库类型" prop="purchase_code" width="200">
                        <template #default="{row}">
                            <el-tag :type="row.input_type == 1 ? 'info' : row.input_type == 2 ? 'primary' : row.input_type == 3 ? 'success' : 'warning'">{{row.input_type == 1 ? '采购单入库' : row.input_type == 2 ? '染色单入库' : row.input_type == 3 ? '加工单入库' : '其他入库'}}</el-tag>
                            <el-link type="primary" @click="table_show_purchase(row)">{{ row.input_type == 1 ? row.purchase?.purchase_code : row.input_input == 2 ? row.dye?.purchase_dye_code: row.process?.purchase_process_code }}</el-link>
                        </template>
                    </el-table-column>
                    <el-table-column label="入库单号" prop="apply.apply_name" width="250">
                        <template #default="{row}">
                            <div style="display: flex;align-items: center;">
                                <el-link type="primary" @click="table_show(row)">{{ row.purchase_ware_code }}</el-link>
                                <el-icon class="icon" color="#1e9fff" :size="20" @click="purchase_staff(row)"><el-icon-BellFilled /></el-icon>
                            </div>
                            <div>{{row.ware_date}}({{row.ware_name}})</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="供应商" prop="date" width="150">
                        <template #default="{row}">
                            <div style="margin: 10px 0;">{{row.apply.apply_name}}</div>
                            <div>{{row.apply.linkman}}</div>
                            单价: 
                        </template>
                    </el-table-column>
                    <el-table-column label="染色方" width="110">
                        <template #default="{row}">
                            <div style="margin: 10px 0;">{{row.dye_factory?row.dyeFactory.apply_name:''}}</div>
                            <div>{{row.dye_factory?row.dyeFactory.linkman:''}}</div>
                            单价: 
                        </template>
                    </el-table-column>
                    <el-table-column label="加工方" width="110">
                        <template #default="{row}">
                            <div style="margin: 10px 0;">{{row.process_factory?row.processFactory.apply_name:''}}</div>
                            <div>{{row.process_factory?row.processFactory.linkman:''}}</div>
                            单价: 
                        </template>
                    </el-table-column>
                    <el-table-column label="品名" prop="goods_name" align="center" width="120"></el-table-column>
                    <el-table-column label="数量(匹)" prop="total_num" align="center" width="120"></el-table-column>
                    <el-table-column label="可染色数量(匹)" prop="can_use_number" align="center" width="140" v-if="name == 'dye'"></el-table-column>
                    <el-table-column label="可加工数量(匹)" prop="can_use_number" align="center" width="140" v-if="name == 'process'"></el-table-column>
                    <el-table-column label="单价(元/米)" prop="price" align="center" width="120"></el-table-column>
                    <el-table-column label="审核" prop="status" width="130" align="center">
                        <template #default="{row}">
                            <el-button :type="row.status == 0 ? 'warning' : row.status == 1 ? 'primary' : 'danger'" size="small" @click="purchase_check(row)" v-auth="'purchasehouse.check'">{{row.status == 0 ? '待审' : row.status == 1 ? '通过' : '退回'}}</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="文件" prop="upload" width="130" align="center">
                        <template #default="{row}">
                            <el-button type="primary" size="small" @click="purchase_upload(row)" v-if="row.status == 1" v-auth="'purchasehouse.upload'">查看</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="备注" prop="remark" align="center" width="120"></el-table-column>
                    <el-table-column label="业务员" prop="createstaff.name" align="center" width="120"></el-table-column>
                    <el-table-column label="操作" fixed="right" align="center" width="230">
                        <template #default="scope">
                            <div class="buttonFlex">
                                <el-button size="small" @click="table_weight_note(scope.row)" v-auth="'purchasehouse.note'">设置码单</el-button>
                                <el-button size="small" @click="table_weight_note_show(scope.row)" v-auth="'purchasehouse.noteshow'">码单</el-button>
                            </div>
                            <div class="buttonFlex">
                                <el-popconfirm :title="`确定${scope.row.is_complate == 1 ? '未完' : '完成'}吗？`" @confirm="purchase_complate(scope.row)">
                                    <template #reference>
                                        <el-button size="small" v-auth="'purchasehouse.complate'">{{scope.row.is_complate == 1 ? '已完成' : '未完'}}</el-button>
                                    </template>
                                </el-popconfirm>
                                <el-button size="small" @click="purchase_print(scope.row)" v-auth="'purchasehouse.print'">打印</el-button>
                            </div>
                            <div class="buttonFlex">
                                <el-button type="primary" size="small" @click="table_edit(scope.row)" v-auth="'purchasehouse.edit'" v-if="scope.row.status != 1">编辑</el-button>
                                <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                    <template #reference>
                                        <el-button type="danger" size="small" v-auth="'purchasehouse.delete'" v-if="scope.row.status != 1">删除</el-button>
                                    </template>
                                </el-popconfirm>
                            </div>
                        </template>
                    </el-table-column> -->
                </scTable>
            </el-main>
        </el-container>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
		</template>
	</el-dialog>
    <check-status v-if="dialog.check" ref="checkDialog" @success="handleSuccess" @closed="dialog.check = false" draggable></check-status>
    <upload-dialog v-if="dialog.upload" ref="uploadDialog" @success="handleSuccess" @closed="dialog.upload = false"></upload-dialog>

    <purchase-save v-if="dialog.purchase" ref="purchaseDialog" @success="handleSuccess" @closed="dialog.purchase = false"></purchase-save>
    <purchase-house v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></purchase-house>
    <dye-save v-if="dialog.dye" ref="dyeDialog" @success="handleSuccess" @closed="dialog.dye = false"></dye-save>
    <dye-house v-if="dialog.dyehouse" ref="dyehouseDialog" @success="handleSuccess" @closed="dialog.dye = false"></dye-house>
    <process-save v-if="dialog.process" ref="processDialog" @success="handleSuccess" @closed="dialog.process = false"></process-save>
    <process-house v-if="dialog.processhouse" ref="processhouseDialog" @success="handleSuccess" @closed="dialog.processhouse = false"></process-house>

    <purchase-weight v-if="dialog.purchase_weight_note" ref="purchaseweightNote" @success="handleSuccess" @closed="dialog.purchase_weight_note = false"></purchase-weight>
    <purchase-print v-if="dialog.print1" ref="print1Dialog" @success="handleSuccess" @closed="dialog.print1 = false"></purchase-print>
    <dye-weight v-if="dialog.dye_weight" ref="dyeWeight" @success="handleSuccess" @closed="dialog.dye_weight = false"></dye-weight>
    <dye-print v-if="dialog.print2" ref="print2Dialog" @success="handleSuccess" @closed="dialog.print2 = false"></dye-print>
    <process-weight v-if="dialog.process_weight" ref="processWeight" @success="handleSuccess" @closed="dialog.process_weight = false"></process-weight>
    <process-print v-if="dialog.print3" ref="print3Dialog" @success="handleSuccess" @closed="dialog.print3 = false"></process-print>
    <el-dialog title="打印" width="500" v-model="dialog.print_select" align-center>
        <el-form label-position="top">
            <el-form-item label="打印关联单">
                <el-select v-model="is_correlation">
                    <el-option label="是" :value="1"></el-option>
                    <el-option label="否" :value="0"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="打印价格"> 
                <el-select v-model="is_price">
                    <el-option label="是" :value="1"></el-option>
                    <el-option label="否" :value="0"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button type="primary" @click="open_print">打 印</el-button>
        </template>
    </el-dialog>
    
    <meter-order v-if="dialog.meter" ref="meterDialog" @success="handleSuccess" @closed="dialog.meter = false"></meter-order>
</template>

<script>
import CheckStatus from './checkStatus.vue'
import uploadDialog from './upload.vue'

import purchaseWeight from '@/views/business/purchase/arrival/weightNote.vue'
import purchasePrint from '@/views/business/purchase/arrival/housePrint.vue'
import dyeWeight from '@/views/business/dye/house/weightNote.vue'
import dyePrint from '@/views/business/dye/house/housePrint.vue'
import processWeight from '@/views/business/machining/house/weightNote.vue'
import processPrint from '@/views/business/machining/house/housePrint.vue'
import MeterOrder from './meterOrder.vue'
import PurchaseHouse from './hosuedialog/purchaseHouse.vue'
import PurchaseSave from './hosuedialog/purchaseSave.vue'
import DyeSave from './hosuedialog/dyeSave.vue'
import DyeHouse from './hosuedialog/dyeHouse.vue'
import ProcessSave from './hosuedialog/processSave.vue'
import ProcessHouse from './hosuedialog/processHouse.vue'

export default {
    props: {
        query: { type: Object, default: () => ({}) },
    },
	emits: ['success', 'closed'],
    components: {
        CheckStatus, uploadDialog, purchaseWeight, purchasePrint, dyeWeight, dyePrint, processWeight, processPrint, MeterOrder, 
        PurchaseHouse, PurchaseSave, DyeSave, DyeHouse, ProcessSave, ProcessHouse
    },
	data() {
		return {
			dialog: {
                save: false,
                check: false,
                purchase: false,
                dye: false,
                dyehouse: false,
                upload: false,
                process: false,
                processhouse: false,
                purchase_weight_note: false,
                print1: false,
                dye_weight: false,
                print2: false,
                process_weight: false,
                print3: false,
                print_select: false,
            },
			mode: "add",
			titleMap: {
				add: '选择入库单',
				edit: '入库记录',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
            apiObj: this.$API.business.purchase.house.list,
            selection: [],
            search: {
                purchase_code: null,
                date: null,
                // status: null,
                goods_name: null,
                input_type: null,
                dye_factory: null,
                process_factory: null,
                action_type: null,
            },
            type: 0,
            applyList: [],
            select: false,
            name: "",
            is_correlation: 1,
            is_price: 1,
            is_apply: 1,
            print_id: "",
            print_type: "",
            column: [
                { label: "入库类型", prop: "input_type", width: "230",align: 'left'},
                { label: "入库单号", prop: "purchase_ware_code", width: "296",align: 'left'},
                { label: "供应商", prop: "apply_name",align: 'left',width: "130"},
                { label: "染色方", prop: "dye_factory",align: 'left'},
                { label: "加工方", prop: "process_factory",align: 'left'},
                { label: "品名", prop: "goods_name", width: "110",},
                { label: "数量(匹)", prop: "total_num", width: "100",},
                { label: "单价(元/米)", prop: "price", width: "110",},
                { label: "审核", prop: "status", width: "90",},
                { label: "文件", prop: "upload", width: "90",},
                { label: "备注", prop: "remark", width: "100",},
                { label: "业务员", prop: "createstaff.name", width: "100",},
                { label: "操作", prop: "operation", width: "230",fixed: 'right'},
            ],
		}
	},
	mounted() {
		// this.getApply();
        if(this.query.input_type){
            this.search.input_type = this.query.input_type
            this.select = true
        }
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
        setData(data,name){
            this.type = data
            this.name = name
            if(name == 'dye'){
                this.search.dye_factory = this.query.dye_factory
            }else {
                this.search.process_factory = this.query.process_factory
            }
        },
        //本地更新数据
        handleSuccess(data, mode) {
            if (mode == 'add') {
                this.$refs.table.refresh()
            } else if (mode == 'edit') {
                this.$refs.table.refresh()
            } else if (mode == 'house') {
                this.$refs.table.refresh()
            } else if (mode == 'dye_house') {
                this.$refs.table.refresh()
            } else if (mode == 'machining_house') {
                this.$refs.table.refresh()
            }
        },
        //查看
        table_show(row) {
            this.dialog[row.input_type == 1 ? 'save' : row.input_type == 2 ? 'dyehouse' : 'processhouse'] = true
            this.$nextTick(() => {
                this.$refs[row.input_type == 1 ? 'saveDialog' : row.input_type == 2 ? 'dyehouseDialog' : 'processhouseDialog'].open('show').setData(row)
            })
        },
        //编辑
        table_edit(row) {
            this.dialog[row.input_type == 1 ? 'save' : row.input_type == 2 ? 'dyehouse' : 'processhouse'] = true
            this.$nextTick(() => {
                this.$refs[row.input_type == 1 ? 'saveDialog' : row.input_type == 2 ? 'dyehouseDialog' : 'processhouseDialog'].open('edit').setData(row)    
            })
        },
        // 采购审核
        purchase_check(row){
            this.dialog.check = true
            this.$nextTick(() => {
                this.$refs.checkDialog.open(row.input_type == 1 ? 'house' : row.input_type == 2 ? 'dye_house' : 'machining_house').setData(row)
            })
        },
        // 查看文件
        purchase_upload(row){
            this.dialog.upload = true
            this.$nextTick(() => {
                this.$refs.uploadDialog.open('show').setData(row)
            })
        },
        // 查看采购单
        table_show_purchase(row){
            let query = {}
            if(row.input_type ==1){
                query = {
                    id: row.purchase_id,
                    purchase_code: row.purchase.purchase_code,
                }
            }else if(row.input_type ==2) {
                query = {
                    id: row.dye_id,
                    purchase_dye_code: row.dye.purchase_dye_code,
                }
            }else {
                query = {
                    id: row.process_id,
                    purchase_process_code: row.process.purchase_process_code,
                }
            }
            this.dialog[row.input_type == 1 ? 'purchase' : row.input_type == 2 ? 'dye' : 'process'] = true
            this.$nextTick(() => {
                this.$refs[row.input_type == 1 ? 'purchaseDialog' : row.input_type == 2 ? 'dyeDialog' : 'processDialog'].open('show').setData(query)
            })
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.search.input_type = this.query.input_type;
            this.$refs.table.upData(this.search)
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        dbselectionChange(selection){
            this.$emit('success', [selection], this.type)
            this.visible = false;
        },
        back(){
            // let array = this.selection.map(item=> item.input_type == 1? item.purchase?.purchase_code : item.dye?.purchase_dye_code)
            // let is_select = array.every(name => name === array[0]);
            // if(!is_select) return this.$message.error(`请选择相同${this.query.input_type == 1 ? '采购' : '染色'}单下的入库单`)
            if(this.name == 'dye'){
                let array = this.selection.map(item=> item.purchase?.purchase_code)
                let is_select = array.every(name => name === array[0]);
                if(!is_select) return this.$message.error("请选择相同采购单下的入库单")
            }
            this.$emit('success', this.selection)
            this.visible = false;
        },


        // 设置码单
        table_weight_note(row){
            this.dialog[row.input_type == 1 ? 'purchase_weight_note' : row.input_type == 2 ? 'dye_weight' : 'process_weight'] = true
            this.$nextTick(() => {
                this.$refs[row.input_type == 1 ? 'purchaseweightNote' : row.input_type == 2 ? 'dyeWeight' : 'processWeight'].open('show').setData(row)
            })
        },
        // 查看码单
        table_weight_note_show(row){
            this.dialog.meter = true
            this.$nextTick(() => {
                this.$refs.meterDialog.open('edit').setData(row)
            })
        },

        // 完成状态切换
        async purchase_complate(row) {
            var reqData = { id: row.id }
            var res = await this.$API.business.purchase.house.complate.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("操作成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },

        // 打印选择弹窗
        purchase_print(row){
            this.print_id = row.id;
            this.print_type = row.input_type;
            this.dialog.print_select = true;
        },
        // 确定打印
        open_print(){
            let query = {
                id: this.print_id,
                is_correlation: this.is_correlation,
                is_price: this.is_price,
            }
            this.dialog.print_select = false;
            this.dialog[this.print_type == 1 ? 'print1' : this.print_type == 2 ? 'print2' : 'print3'] = true
            this.$nextTick(() => {
                this.$refs[this.print_type == 1 ? 'print1Dialog' : this.print_type == 2 ? 'print2Dialog' : 'print3Dialog'].open('show').setData(query)
            })
            this.is_correlation = 1
            this.is_price = 1
        },


        //删除
        async table_del(row) {
            var reqData = { id: row.id }
            var res = await this.$API.business.purchase.house.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },

	}
}
</script>

<style lang="scss">
.el-input-group__append{
    padding: 0 8px;
}
</style>
